package com.confinderence;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.confinderence.WebServiceTask.IResponseHandler;
import com.confinderence.model.Conference;
import com.confinderence.model.Lecture;
import com.confinderence.model.Lecturer;
import com.confinderence.model.User;
import com.google.gson.Gson;

public class RegisterActivity extends Activity implements IResponseHandler {
	private EditText mNickname;
	private EditText mPassword;
	private EditText mConfirmPassword;
	private EditText mEmail;
	private EditText mName;
	private EditText mBiography;
	private Button mRegBtn;
	private Lecturer mLecturer;

	private static final String URL = "http://" + Utils.IP + "/ConferenceManager/rest/lecturer";
	private WebServiceTask mWebTask;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_register);
		init();
	}
	private void init() {
		mNickname = (EditText) findViewById(R.id.regNicknameView);
		mPassword = (EditText) findViewById(R.id.regPasswordView);
		mConfirmPassword = (EditText) findViewById(R.id.regConfirmPassView);
		mEmail = (EditText) findViewById(R.id.regEmailView);
		mName = (EditText) findViewById(R.id.regNameView);
		mBiography = (EditText) findViewById(R.id.regBiographyView);
		mRegBtn = (Button) findViewById(R.id.regButton);
		mRegBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String username = mNickname.getText().toString();
				String password = mPassword.getText().toString();
				String email = mEmail.getText().toString();
				String confirmPassword = mConfirmPassword.getText().toString();
				String biography = mBiography.getText().toString();
				String name = mName.getText().toString();
				if (!password.equals(confirmPassword)) {
					Toast.makeText(RegisterActivity.this, getString(R.string.invalid_input),
							Toast.LENGTH_LONG).show();
				} else {
					mLecturer = new Lecturer(username, password, email, name, biography,
							new ArrayList<Lecture>(), new ArrayList<Conference>());
					mLecturer.setActive(true);
					registerLecturer(mLecturer);
				}
			}
		});
	}
	protected void registerLecturer(Lecturer lecturer) {
		mWebTask = new WebServiceTask(WebServiceTask.POST_TASK_WITH_JSON, this,
				getString(R.string.wait_msg), this);
		Gson gson = new Gson();
		String json = gson.toJson(lecturer);
		json = "{" + "\"lecturer\" : " + json + "}";
		mWebTask.setJson(json);
		mWebTask.execute(URL);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.logged_in_menu, menu);
		Utils.initMenu(menu, this);
		return true;
	}
	@Override
	public void handleResponse(String response) {
		if (response.equals("Registration Successful!")) {
			Utils.signInUser(new User(mLecturer.getUsername(), mLecturer.getPassword()), this);
			Utils.setLecturer(mLecturer);
			Intent intent = new Intent(this, ConferenceActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(intent);
		}
	}
}
